package com.knife.blog.web.action;

import com.knife.util.CommUtil;
import com.knife.util.HtmlUtil;
import com.knife.util.TagUtil;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;
import com.knife.blog.domain.BlogMessage;
import com.knife.blog.domain.BlogUser;
import com.knife.blog.logic.BlogMessageService;
import com.knife.blog.logic.HTMLGenerater;
import com.knife.blog.logic.impl.BlogMessageServiceImpl;
import java.net.URLDecoder;
import java.net.URLEncoder;
import org.apache.log4j.Logger;

public class BlogMessageAction extends BaseCrudAction {
    private static final Logger logger = (Logger) Logger.getLogger(BlogMessageAction.class.getName());
    private BlogMessageService blogMessageService = BlogMessageServiceImpl.getInstance();

    public IPageList doQuery(WebForm form, int currentPage, int pageSize) {
        String belonguser = null;
        try {
            belonguser = URLDecoder.decode(URLEncoder.encode((String) form.get("user"), "ISO8859_1"), "utf-8");
        } catch (Exception localException) {
        }
        if (belonguser == null) {
            belonguser = (String) form.get("belonguser");
        }
        if ((belonguser == null) && (getCurrentUser(form) != null)) {
            belonguser = getCurrentUser(form).getUserName();
        }
        form.addResult("belonguser", belonguser);
        this.blog = this.userService.getUserBlog(belonguser);
        return this.blogMessageService.queryBlogMessage(this.blog.getCid(), currentPage,
                pageSize);
    }

    public Page doList(WebForm form, Module module) {
        String belonguser = null;
        try {
            belonguser = URLDecoder.decode(URLEncoder.encode((String) form.get("user"), "ISO8859_1"), "utf-8");
        } catch (Exception localException) {
        }
        if (belonguser == null) {
            belonguser = (String) form.get("belonguser");
        }
        if ((belonguser == null) && (getCurrentUser(form) != null)) {
            belonguser = getCurrentUser(form).getUserName();
        }
        int currentPage = CommUtil.null2Int(form.get("page"));
        int PageSize = CommUtil.null2Int(form.get("PageSize"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (PageSize < 1) {
            PageSize = 15;
        }
        this.blog = this.userService.getUserBlog(belonguser);
        try {
            IPageList pList = this.blogMessageService.queryBlogMessage(this.blog.getCid(), currentPage,PageSize);
            form.addResult("belonguser", belonguser);
            form.addResult("list", pList.getResult());
            form.addResult("pages", new Integer(pList.getPages()));
            form.addResult("rows", new Integer(pList.getRowCount()));
            form.addResult("page", new Integer(pList.getCurrentPage()));
            //form.addResult("gotoPageHTML", CommUtil.showPageHtml(pList.getCurrentPage(), pList.getPages()));
            form.addResult("gotoPageHTML", TagUtil.showPageHtml(pList.getCurrentPage(), pList.getPages()));
        } catch (Exception e) {
            System.out.println("获取评论列表信息时出现了异常,请检查系统日志");
            logger.error(e);
        }
        return module.findPage("list");
    }

    public Page doUlist(WebForm form, Module module) {
        String belonguser = null;
        try {
            belonguser = URLDecoder.decode(URLEncoder.encode((String) form.get("user"), "ISO8859_1"), "utf-8");
        } catch (Exception localException) {
        }
        if (belonguser == null) {
            belonguser = (String) form.get("belonguser");
        }
        if ((belonguser == null) && (getCurrentUser(form) != null)) {
            belonguser = getCurrentUser(form).getUserName();
        }
        int currentPage = CommUtil.null2Int(form.get("page"));
        int PageSize = CommUtil.null2Int(form.get("PageSize"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (PageSize < 1) {
            PageSize = 15;
        }
        this.blog = this.userService.getUserBlog(belonguser);
        try {
            IPageList pList = this.blogMessageService.queryBlogUnMessage(this.blog.getCid(), currentPage,PageSize);
            form.addResult("belonguser", belonguser);
            form.addResult("list", pList.getResult());
            form.addResult("pages", new Integer(pList.getPages()));
            form.addResult("rows", new Integer(pList.getRowCount()));
            form.addResult("page", new Integer(pList.getCurrentPage()));
            //form.addResult("gotoPageHTML", CommUtil.showPageHtml(pList.getCurrentPage(), pList.getPages()));
            form.addResult("gotoPageHTML", TagUtil.showPageHtml(pList.getCurrentPage(), pList.getPages()));
        } catch (Exception e) {
            System.out.println("获取评论列表信息时出现了异常,请检查系统日志");
            logger.error(e);
        }
        return module.findPage("ulist");
    }

    public Page doPost(WebForm form, Module module) {
        BlogMessage obj = (BlogMessage) form2Obj(form);
        obj.setBelonguser((String) form.get("belongCid"));
        obj.setAuditing(Integer.valueOf(0));
        boolean ret = this.blogMessageService.add(obj);
        form.addResult("msg", (ret) ? "留言成功" : "留言失败");
        String clientUrl = "/" + this.blog.getDomain() + "/message.htm";
        form.addResult("clientUrl", clientUrl);
        return new Page("clientforward", "/manage/forward.html", "template");
    }

    @Override
    public Page doAdd(WebForm form, Module module, IActiveUser user) {
        BlogMessage obj = (BlogMessage) form2Obj(form);
        obj.setBelonguser((String) form.get("belongCid"));
        obj.setAuditing(Integer.valueOf(1));
        boolean ret = this.blogMessageService.add(obj);
        if (ret) {
            form.addResult("msg", "添加成功!");
            return super.doQuery(form, module, user);
        }
        form.addResult("msg", "添加失败!");
        return module.findPage("edit");
    }

    public Page doAuditing(WebForm form, Module module) {
        BlogMessage message = this.blogMessageService.getBlogMessage((String) form.get("cid"));
        message.setAuditing(new Integer(1));
        boolean ret = this.blogMessageService.updateBlogMessage(message);
        if (ret) {
            form.addResult("msg", "审核成功！");
            this.userService.blogInfoChange(this.blog);
            try {
                HTMLGenerater.saveMessageToHTML(this.blog);
            } catch (Exception e) {
                //e.printStackTrace();
                System.out.println("生成留言出现异常,请检查系统日志");
                logger.error(e);
            }
        }
        IActiveUser user = getCurrentUser(form);
        return super.doQuery(form, module, user);
    }

    @Override
    public Page doDel(WebForm form, Module module, IActiveUser user) {
        BlogMessage obj = (BlogMessage) form2Obj(form);
        boolean ret = this.blogMessageService.delBlogMessage(obj.getCid());
        form.addResult("msg", (ret) ? "删除成功!" : "删除失败!");
        HTMLGenerater.saveMessageToHTML(blog);
        com.knife.blog.logic.HTMLGenerater.saveToHTML(blog);
        return super.doQuery(form, module, user);
    }

    @Override
    public Page doUpdate(WebForm form, Module module, IActiveUser user) {
        BlogMessage obj = (BlogMessage) form2Obj(form);
        boolean ret = this.blogMessageService.updateBlogMessage(obj);
        form.addResult("msg", (ret) ? "修改成功!" : "修改失败!");
        return super.doQuery(form, module, user);
    }

    public Page doReviewOld(WebForm form, Module module) {
        BlogMessage message = this.blogMessageService.getBlogMessage((String) form.get("cid"));
        String review = (String) form.get("review");
        message.setReview(review);
        boolean ret = this.blogMessageService.updateBlogMessage(message);
        IActiveUser user = getCurrentUser(form);
        if (ret) {
            this.userService.blogInfoChange(this.blog);
            form.addResult("msg", "回复成功！");
        } else {
            form.addResult("msg", "回复失败！");
        }
        return super.doQuery(form, module, user);
    }

    public Page doReview(WebForm form, Module module) {
        BlogMessage message = this.blogMessageService.getBlogMessage((String) form.get("cid"));
        String review = (String) form.get("review");
        message.setReview(review);
        boolean ret = this.blogMessageService.updateBlogMessage(message);
        IActiveUser user = getCurrentUser(form);
        if (ret) {
            this.userService.blogInfoChange(this.blog);
            form.addResult("msg", "回复成功！");
        } else {
            form.addResult("msg", "回复失败！");
        }
        return super.doQuery(form, module, user);
    }

    public Page doCountUnCheck(WebForm form, Module module){
        int ret=0;
        ret = blogMessageService.countUnCheckMessage(blog.getCid());
        form.addResult("result", ret);
        return module.findPage("ajaxResult");
    }
    
    public Page doSetting(WebForm form, Module module) {
        return module.findPage("setting");
    }

    public Object form2Obj(WebForm form) {
        String cid = CommUtil.null2String(form.get("cid"));
        BlogMessage obj = this.blogMessageService.getBlogMessage(cid);
        if (obj == null) {
            obj = new BlogMessage();
        }
        form.toPo(obj);
        obj.setBelonguser(HtmlUtil.escapeHTMLTag(obj.getBelonguser()));
        obj.setCid(HtmlUtil.escapeHTMLTag(obj.getCid()));
        obj.setContent(HtmlUtil.escapeHTMLTag(obj.getContent()));
        obj.setInputuser(HtmlUtil.escapeHTMLTag(obj.getInputuser()));
        return obj;
    }
}